package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Flight;
import model.FlightIO;

/**
 * Servlet implementation class ViewAndBook
 */
public class ViewAndBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewAndBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * On a post, the ViewAndBook servlet get the classType, flight, seat 
	 * number from the session. The flight then checks to make sure the number
	 * of seats are still available. The total cost is calculated. If the 
	 * number of seats are still available then the request is forwarded to
	 * Transaction.jsp. If not, it goes back to flight search results with the
	 * error message. 
	 *  
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Get session object
		HttpSession session = request.getSession();
		FlightIO flightIO = new FlightIO();
		ArrayList<Flight> cart = (ArrayList<Flight>) session.getAttribute("shoppingCart");
		
		boolean added = true;
		double totalCost = 0;
		//Verify number of requested seats
		for(int i=0; i<cart.size(); i++){
			Flight flight = cart.get(i);
			int seats = flight.getSeats();
			int planeID = flight.getPlaneId();
			String classType = flight.getSelectedClass();
			totalCost += (flight.getCost() * flight.getSeats());
			int reservedSeats = 0;
			if(classType.equals("Economy")){
				reservedSeats = flight.getEconomy();
				classType = "economy";
			} else if(classType.equals("Business")){
				reservedSeats = flight.getBusiness();
				classType = "business";
			} else {
				reservedSeats = flight.getFirstClass();
				classType = "first_class";
			}
			added = added && flightIO.checkSeats(seats, planeID, reservedSeats, classType);
		}
		session.setAttribute("totalCost", totalCost);
		if(added){
			//Forward to FlightSearchResults
			//Encode URL
			String url = "/WEB-INF/ConfirmBooking.jsp";
			url = response.encodeURL(url);
			RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
			rd.forward(request, response);
		} else {
			//Encode URL
			String url = "ShoppingCart.jsp";
			url = response.encodeURL(url);
			RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
			rd.forward(request, response);
		}
	
	}
}
